<template>
	<div class="">
		<image :src="banner" mode="widthFix" class="banner"></image>
		<div class="fixBottom">
			<div class="container">
				<strong>下载方法：</strong>点击下载，分享给文件传输助手，即可资料下载
			</div>
		</div>
		<u-sticky>
			<div class="filterBox">
				<u-tabs :list="list1" @click="changeType()"></u-tabs>
			</div>
		</u-sticky>
		<div class="container">
			<div class="list">
				
				<div class="item" v-for="(item,index) in showList" :key="index" @click="checkPdf(item)">
					<div class="img">
						<image src="https://www.baixuemianshi.com/zhuanti/images/pdf2.png?V1" mode="widthFix"></image>
					</div>
					<div class="text">
						<h3>{{ item.title }}</h3>
						<div class="bottom">
							<div class="time">{{ item.time }}</div>
							<div class="btns">
								<div class="btn" @click.stop="downloadPdf(item)">
									<u-button type="success" size="mini" text="下载"></u-button>
								</div>
								<div class="btn">
									<u-button type="error" size="mini" text="查看"></u-button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="fixBottom">
			<div class="container">
				<strong>下载方法：</strong>点击下载，分享给文件传输助手，即可进行资料下载
			</div>
		</div>
	</div>
</template>

<script>
const db = wx.cloud.database();
export default {
	data() {
		return {
			banner:"https://www.baixuemianshi.com/zhuanti/images/banner_jx.jpg?" + new Date(),
			openid:"",
			userinfo:{
				_id:"",
				_openid:"",
				readList:[]
			},
			zlList:[],
			list1:[
				{name:"全部内容"}
			],
			selectType:""
		};
	},
	onLoad(options) {
		
	},
	computed: {
		showList(){
			let arr = []
			if(this.selectType){
				arr = this.zlList.filter(item => item.type == this.selectType)
			}else{
				arr = this.zlList
			}
			return arr
		}
	},
	onShareAppMessage(options) {},
	onShareTimeline(res) {},
	mounted() {
		this.getList(0)
	},
	watch: {},
	created() {
		this.getType()
		this.getopenid()
	},
	methods: {
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					that.openid = res.result.openid
					that.getUserInfo()
				}
			});
		},
		getUserInfo(){
			let that = this
			db.collection("jx_zzfx_user").where({_openid:that.openid}).get({
				success: res => {
					if(res.data.length > 0){
						that.userinfo = res.data[0]
					}else{
						db.collection("jx_zzfx_user").add({
							data:{
								readList:[]
							},
							success:res2 => {
								that.userinfo._id = res2._id
							}
						})
					}
				}
			})
		},
		getType(){
			let that = this
			uni.request({
				url: "https://www.baixuemianshi.com/zhuanti/upload_pdf/js/type.js",
				method: 'get',
				dataType: 'text',
				data: {
					sstimes: new Date()
				},
				success: function(res) {
					console.log(res)
					var resjson = JSON.parse(res.data);
					that.list1 = that.list1.concat(resjson)
				}
			});
		},
		changeType(e){
			console.log(e)
			if(e.name == "全部内容"){
				this.selectType = ""
			}else{
				this.selectType = e.name
			}
			
		},
		getList(page){
			let that = this
			db.collection("jx_zzfx_pdf").orderBy('time','desc').skip(page * 20).limit(20).get({
				 success:res => {
					 if(res.data.length > 0){
						 that.zlList = that.zlList.concat(res.data)
						 let p = page + 1
						 that.getList(p)
					 }else{
						 
					 }
					 
				 }
			})
		},
		checkPdf(info){
			let that = this
			let list = that.userinfo.readList
			let reads = list.filter(item => item._id == info._id)
			if(reads.length == 0){
				that.userinfo._openid = that.openid
				that.userinfo.readList.push(info)
				db.collection("jx_zzfx_user").doc(that.userinfo._id).update({
					data:{
						readList: that.userinfo.readList
					},
					success: res => {
						that.viewPdf(info)
					}
				})
			}else{
				that.viewPdf(info)
			}
		},
		viewPdf(info){
			let that = this
			let link = 'https://www.baixuemianshi.com/zhuanti/pdf_view3/web/?file=' + info.link
			uni.navigateTo({
				url:"../../webview/index?href=" + encodeURIComponent(link)
			})
		},
		downloadPdf(item){
			let that = this
			wx.showLoading()
			wx.downloadFile({
				url: item.link, // 地址已打码，自己换个其他的地址("https://www.xxxxx.com/file/测试通知.pdf")
				success: function (res) {
					console.log(res, "wx.downloadFile success res")
					if(res.statusCode != 200) {
						console.log("error")
						return false
					}
					var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
					wx.shareFileMessage({
						filePath: Path,
						showMenu: false,
						success: function (res) {
							console.log('分享成功');
							wx.hideLoading()
						}
					})
				},
				fail: function (err) {
					console.log(err, "wx.downloadFile fail err");
				}
			})
		}
	}
};
</script>

<style>
  @import url('./index.css');
</style>
